package com.atguigu.gmall.controller;

import com.atguigu.gmall.bean.KeywordStats;
import com.atguigu.gmall.bean.ProductStats;
import com.atguigu.gmall.bean.ProvinceStats;
import com.atguigu.gmall.bean.VisitorStats;
import com.atguigu.gmall.service.KeywordStatsService;
import com.atguigu.gmall.service.ProductStatsService;
import com.atguigu.gmall.service.ProvinceStatsService;
import com.atguigu.gmall.service.VisitorStatsService;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.*;

/**
 * @author: xu
 * @desc: 大屏展示的控制层
 * 主要职责：接收客户端的请求(request)，对请求进行处理，并给客户端响应(response)
 */
@RestController
@RequestMapping("/api/sugar")
public class SugarController {
    @Autowired
    ProductStatsService productStatsService;
    @Autowired
    ProvinceStatsService provinceStatsService;
    @Autowired
    VisitorStatsService visitorStatsService;
    @Autowired
    KeywordStatsService keywordStatsService;

    @RequestMapping("/keyword")
    public String getKeywordStats(@RequestParam(value = "date", defaultValue = "0") Integer date,
                                  @RequestParam(value = "limit", defaultValue = "20") int limit) {
        if (date == 0) date = now();
        // 查询数据
        List<KeywordStats> keywordStatsList = keywordStatsService.getKeywordStats(date, limit);
        StringBuilder builder = new StringBuilder("{\"status\":0,\"msg\":\"\",\"data\":[");
        // 循环拼接字符串
        for (int i = 0; i < keywordStatsList.size(); i++) {
            KeywordStats keywordStats = keywordStatsList.get(i);
            if (i >= 1) builder.append(",");
            builder.append("{\"name\":\"").append(keywordStats.getKeyword()).append("\",").append("\"value\":").append(keywordStats.getCt()).append("}");
        }
        builder.append("]}");
        return builder.toString();
    }


    @RequestMapping("/hr")
    public String getVisitorStatsByHr(@RequestParam(value = "date", defaultValue = "0") Integer date) {
        if (date == 0) date = now();
        // 从service层中获取分时访问数据
        List<VisitorStats> visitorStatsByHrList = visitorStatsService.getVisitorStatsByHr(date);
        // 因为有的小时可能没有数据，为了把每个小时都展示出来，我们创建一个数组，用来保存每个小时对应的访问情况
        VisitorStats[] visitorStatsArr = new VisitorStats[24];
        for (VisitorStats visitorStats : visitorStatsByHrList) {
            visitorStatsArr[visitorStats.getHr()] = visitorStats;
        }
        // 定义存放小时、uv、pv、新用户的List集合
        List<String> hrList = new ArrayList<>();
        List<Long> uvList = new ArrayList<>();
        List<Long> pvList = new ArrayList<>();
        List<Long> newVisitorList = new ArrayList<>();

        // 对数组进行遍历，将0~23点的数据查询出来，分别放到对应的List集合中保存起来
        for (int i = 0; i <= 23; i++) {
            VisitorStats visitorStats = visitorStatsArr[i];
            if (visitorStats != null) {
                uvList.add(visitorStats.getUv_ct());
                pvList.add(visitorStats.getPv_ct());
                newVisitorList.add(visitorStats.getNew_uv());
            } else {
                uvList.add(0L);
                pvList.add(0L);
                newVisitorList.add(0L);
            }
            // 小时位不足2位的时候，前面补0
            hrList.add(String.format("%02d", i));
        }
        // 拼接字符串
        return "{\"status\":0,\"data\":{" + "\"categories\":" +
                "[\"" + StringUtils.join(hrList, "\",\"") + "\"],\"series\":[" +
                "{\"name\":\"uv\",\"data\":[" + StringUtils.join(uvList, ",") + "]}," +
                "{\"name\":\"pv\",\"data\":[" + StringUtils.join(pvList, ",") + "]}," +
                "{\"name\":\"新用户\",\"data\":[" + StringUtils.join(newVisitorList, ",") + "]}]}}";
    }


    @RequestMapping("/visitor")
    public String getVisitorStatsByNewFlag(@RequestParam(value = "date", defaultValue = "0") Integer date) {
        if (date == 0) date = now();
        // 调用service层，获取访客统计相关指标数据
        List<VisitorStats> visitorStatsByNewFlagList = visitorStatsService.getVisitorStatsByNewFlag(date);
        // 定义两个对象，分别接收新老访客统计的结果
        VisitorStats newVisitorStats = new VisitorStats();
        VisitorStats oldVisitorStats = new VisitorStats();
        // 对查询的数据进行遍历，给新老访客统计对象赋值
        for (VisitorStats visitorStats : visitorStatsByNewFlagList) {
            if ("1".equals(visitorStats.getIs_new())) {
                newVisitorStats = visitorStats;
            } else {
                oldVisitorStats = visitorStats;
            }
        }

        return "{\"status\": 0,\"data\": " +
                "{\"columns\": [" +
                "{\"name\": \"类别\",\"id\": \"type\"}," +
                "{\"name\": \"新用户\",\"id\": \"new\"}," +
                "{\"name\": \"老用户\",\"id\": \"old\"}]," +
                "\"rows\": [" +
                "{\"type\": \"用户数(人)\",\"new\": " + newVisitorStats.getUv_ct() + ",\"old\": " + oldVisitorStats.getUv_ct() + "}," +
                "{\"type\": \"总访问页面(次)\",\"new\": " + newVisitorStats.getPv_ct() + ",\"old\": " + oldVisitorStats.getPv_ct() + "}," +
                "{\"type\": \"跳出率(%)\",\"new\": " + newVisitorStats.getUjRate() + ",\"old\": " + oldVisitorStats.getUjRate() + "}," +
                "{\"type\": \"平均在线时长(秒)\",\"new\": " + newVisitorStats.getDurPerSv() + ",\"old\": " + oldVisitorStats.getDurPerSv() + "}," +
                "{\"type\": \"平均访问页面数(次)\",\"new\": " + newVisitorStats.getPvPerSv() + ",\"old\": " + oldVisitorStats.getPvPerSv() + "}]}}";
    }

    /**
     * {
     * "status": 0,
     * "data": {"mapData": [{"name": "北京","value": 7489 }] }
     * }
     */
    @RequestMapping("/province")
    public String getProvinceStats(@RequestParam(value = "date", defaultValue = "0") Integer date) {
        if (date == 0) date = now();
        // 从service中获取地区统计数据
        List<ProvinceStats> provinceStatsList = provinceStatsService.getProvinceStats(date);
        StringBuilder jsonBuilder = new StringBuilder("{\"status\": 0,\"data\": {\"mapData\": [");
        for (int i = 0; i < provinceStatsList.size(); i++) {
            ProvinceStats provinceStats = provinceStatsList.get(i);
            if (i >= 1) {
                jsonBuilder.append(",");
            }
            jsonBuilder.append("{\"name\": \"").append(provinceStats.getProvince_name()).append("\",\"value\": ").append(provinceStats.getOrder_amount()).append("}");
        }
        jsonBuilder.append("]}}");
        return jsonBuilder.toString();
    }

    /**
     * {
     * "status": 0,
     * "data": {
     * "columns": [
     * {"name": "商品SPU名称","id": "spu_name"},
     * {"name": "交易额", "id": "order_amount"},
     * ],
     * "rows": [{"spu_name": "XXX", "order_amount": "XXX"}]
     * }
     * }
     */
    @RequestMapping("/spu")
    public String getProductStatsBySPU(
            @RequestParam(value = "date", defaultValue = "0") Integer date,
            @RequestParam(value = "limit", defaultValue = "10") Integer limit) {
        if (date == 0) date = now();
        // 调用service层方法，获取按spu统计数据
        List<ProductStats> productStatsBySPUList = productStatsService.getProductStatsBySPU(date, limit);
        // 初始化表头信息
        StringBuilder jsonBuilder = new StringBuilder("{" +
                "\"status\": 0," +
                "\"data\": {" +
                "\"columns\": [{" +
                "\"name\": \"商品SPU名称\"," +
                "\"id\": \"spu_name\"" +
                "}," +
                "{" +
                "\"name\": \"交易额\"," +
                "\"id\": \"order_amount\"" +
                "}," +
                "{" +
                "\"name\": \"订单数\"," +
                "\"id\": \"order_ct\"" +
                "}" +
                "]," +
                "\"rows\": [");
        // 对查询出来的数据进行遍历，将每一条遍历的结果封装为json的一行数据
        for (int i = 0; i < productStatsBySPUList.size(); i++) {
            ProductStats productStats = productStatsBySPUList.get(i);
            if (i >= 1) {
                jsonBuilder.append(",");
            }
            jsonBuilder.append("{" + "\"spu_name\": \"").append(productStats.getSpu_name()).append("\",").append("\"order_amount\":").append(productStats.getOrder_amount()).append(",").append("\"order_ct\":").append(productStats.getOrder_ct()).append("}");
        }
        jsonBuilder.append("]}}");
        return jsonBuilder.toString();
    }

    /**
     * {
     * "status": 0,
     * "data": [
     * {"name": "PC", "value": 97},
     * {"name": "iOS","value": 50}
     * ]
     * }
     */
    @RequestMapping("/category3")
    public Map getProductStatsByCategory3(
            @RequestParam(value = "date", defaultValue = "0") Integer date,
            @RequestParam(value = "limit", defaultValue = "10") Integer limit) {
        if (date == 0) date = now();
        // 调用service获取品类交易额排行
        List<ProductStats> productStatsByCategory3List = productStatsService.getProductStatsByCategory3(date, limit);
        Map resMap = new HashMap();
        List dataList = new ArrayList();
        for (ProductStats productStats : productStatsByCategory3List) {
            Map dataMap = new HashMap();
            dataMap.put("name", productStats.getCategory3_name());
            dataMap.put("value", productStats.getOrder_amount());
            dataList.add(dataMap);
        }
        resMap.put("status", 0);
        resMap.put("data", dataList);
        return resMap;
    }

    /**
     * {
     * "status": 0,
     * "data": {
     * "categories": ["苹果","三星","华为"],
     * "series": [{"data": [9387,8095,8863]}]
     * }
     * }
     */
    /**
     * 封装对象，通过将对象转换的json格式字符串的方式 返回json数据
     */
    @RequestMapping("/trademark")
    public String getProductStatsByTrademark(
            @RequestParam(value = "date", defaultValue = "0") Integer date,
            @RequestParam(value = "limit", defaultValue = "10") Integer limit) {
        // 如果没有传递日期参数，那么将日期设置为当前日期
        if (date == 0) date = now();
        // 调用service根据品牌获取交易额排名
        List<ProductStats> productStatsByTrademarkList = productStatsService.getProductStatsByTrademark(date, limit);
        // 定义两个集合，分别存放品牌的名称以及品牌的交易额
        List<String> trademarkNameList = new ArrayList<>();
        List<BigDecimal> amountList = new ArrayList<>();
        // 对获取到的品牌交易额进行遍历
        for (ProductStats productStats : productStatsByTrademarkList) {
            trademarkNameList.add(productStats.getTm_name());
            amountList.add(productStats.getOrder_amount());

        }

        return "{\"status\": 0,\"data\": " +
                "{\"categories\": [\"" + StringUtils.join(trademarkNameList, "\",\"") + "\"]," +
                "\"series\": [{\"name\": \"商品品牌\",\"data\": [" + StringUtils.join(amountList, ",") + "]}]}}";
    }

    /**
     * {
     * "status": 0,
     * "msg": "",
     * "data": 1201076.1961842624
     * }
     */
    @RequestMapping("/gmv")
    public String getGMV(@RequestParam(value = "date", defaultValue = "0") Integer date) {
        if (date == 0) date = now();
        BigDecimal gmv = productStatsService.getGMV(date);
        return "{\"status\": 0,\"data\": " + gmv + "}";
    }

    private Integer now() {
        String yyyyMMdd = DateFormatUtils.format(new Date(), "yyyyMMdd");
        return Integer.valueOf(yyyyMMdd);
    }
}